﻿using Autofac;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Common.Tools
{
    /// <summary>
    /// Autofac注册
    /// </summary>
    public class AutofacModuleRegister : Autofac.Module
    {
        private string _interfaceDllName;
        private string _instanceDllName;
        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="interfaceDllName">接口dll名称</param>
        /// <param name="instanceDllName">实现dll名称</param>
        public AutofacModuleRegister(string interfaceDllName, string instanceDllName)
        {
            _interfaceDllName = interfaceDllName;

            _instanceDllName = instanceDllName;

        }
        protected override void Load(ContainerBuilder builder)
        {
            //利用反射加载需要注入的服务(必须先引用dll)
            Assembly InterfaceAssembly = Assembly.Load(_interfaceDllName);
            Assembly ServiceAssembly = Assembly.Load(_instanceDllName);
            //注入
            builder.RegisterAssemblyTypes(InterfaceAssembly, ServiceAssembly).AsImplementedInterfaces();
        }
    }
}
